package vue;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * Fenetre d'initialisation de la simulation
 *
 */
public class BoiteInitialisation{
	int nbBebetesInterface;
	int nbChampiInterface;
	int delaisInterface;
	public BoiteInitialisation() {
		final JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setTitle("Creation de la simulation");
		JPanel config = new JPanel();
		config.setPreferredSize(new Dimension(100, 60));
		config.setBorder(BorderFactory.createTitledBorder("Parametre : "));
		
		/*
		 * Recuperation du nombre de bebete
		 */
		JLabel nbBebetes = new JLabel("Nombre de bebetes");
		final JTextField nbBebetesChoisi = new JTextField("50");
		nbBebetesChoisi.setPreferredSize(new Dimension(50, 25));
		
		/*
		 * Recuperation du nombre de champi
		 */
		JLabel nbChampi = new JLabel("Nombre de champi");
		final JTextField nbChampiChoisi = new JTextField("1");
		nbChampiChoisi.setPreferredSize(new Dimension(50, 25));
		
		/*
		 * Recuperation du delais de la simulation
		 */
		JLabel delais = new JLabel("Delais de deplacement");
		final JTextField delaisChoisi = new JTextField("50");
		nbBebetesChoisi.setPreferredSize(new Dimension(50, 25));
		JButton envoyer = new JButton("Valider");
		envoyer.addActionListener(new ActionListener() {
	      public void actionPerformed(ActionEvent e) {
	    	  f.setVisible(false);
	    	  try
	    	  {
	    		 nbBebetesInterface = Integer.parseInt(nbBebetesChoisi.getText());
	    	  }
	    	  catch (NumberFormatException n)
	    	  {
	    		  nbBebetesInterface = 50;
	    		  System.out.println("Erreur, 50 bebete choisi par defaut");
	    	  }
	    	  try
	    	  {
	    		 delaisInterface = Integer.parseInt(delaisChoisi.getText());
	    	  }
	    	  catch (NumberFormatException n)
	    	  {
	    		 delaisInterface = 50;
	    		  System.out.println("Erreur, 50ms de delai choisi par defaut");
	    	  }
	    	  try
	    	  {
	    		 nbChampiInterface = Integer.parseInt(nbChampiChoisi.getText());
	    	  }
	    	  catch (NumberFormatException n)
	    	  {
	    		  nbChampiInterface = 1;
	    		  System.out.println("Erreur, 1 champi choisi par defaut");
	    	  }
	    	  
	    	  InterfaceGraphique IntefaceSimu = new InterfaceGraphique(nbBebetesInterface, nbChampiInterface, delaisInterface);
	      }
		});
		GridLayout calque = new GridLayout(0,2);
		calque.setHgap(1);
		config.setLayout(calque);
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		config.add(nbBebetes);
		config.add(nbBebetesChoisi);
		config.add(delais);
		config.add(delaisChoisi);
		config.add(nbChampi);
		config.add(nbChampiChoisi);
		config.add(new JLabel());
		config.add(envoyer);
		f.setLocation(dim.width / 2, dim.height / 2);
		f.add(config);
		f.setSize(300,150);
		f.setVisible(true);
	}

}
